package com.ph.permission.api.controller;

import java.util.ArrayList;
import java.util.List;

import com.ph.shopping.common.core.customenum.RoleEnum;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.ph.base.BaseController;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.util.page.PageBean;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.permission.dto.RoleDto;
import com.ph.shopping.facade.permission.dto.RoleMenuDto;
import com.ph.shopping.facade.permission.entity.Role;
import com.ph.shopping.facade.permission.service.IMenuService;
import com.ph.shopping.facade.permission.service.IRoleService;

/**
 * @项目：phshopping-api-platform
 *
 * @描述：角色controller
 *
 * @作者： Mr.chang
 *
 * @创建时间：2017年3月13日
 *
 * @Copyright @2017 by Mr.chang
 */
@Controller
@RequestMapping(value="api/role")
public class RoleController extends BaseController{
	
	private static Logger logger = LoggerFactory.getLogger(RoleController.class);

	@Reference(version="1.0.0")
	private IRoleService roleService;
	
	@Reference(version="1.0.0")
	private IMenuService menuService;
	
	/**
	 * 分页获取角色列表
	 * @return
	 */
	@RequestMapping(value="/list",method= RequestMethod.GET)
	@ResponseBody
	public Object list(@ModelAttribute PageBean page,@ModelAttribute RoleDto role){
		
		logger.info("分页获取角色列表入参，page={},role={}",JSON.toJSONString(page),JSON.toJSONString(role));
		Result result=roleService.getRoleByPage(page,role);
		logger.info("分页获取角色列表返回值，result={}",JSON.toJSONString(result));
		return result;	
	}
	
	/**
	 * 获取所有角色
	 * @return
	 */
	@RequestMapping(value="/list/all",method= RequestMethod.GET)
	@ResponseBody
	public Object listAll(){
		//查询管理员类型的roleCode
		Result result=roleService.getAllRole();
		return result;	
	}

	/**
	 * 新增角色
	 * @param role
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Object add(@ModelAttribute Role role){
		role.setRoleCode(Byte.valueOf(RoleEnum.ADMIN.getCode() + ""));
		return roleService.addRole(role);
	}

	/**
	 * 修改角色
	 * @param role
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	@ResponseBody
	public Object update(@ModelAttribute Role role){
		return roleService.updateRole(role);
	}

	/**
	 * 删除角色
	 * @param role
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Object delete(@ModelAttribute Role role){
		return roleService.deleteRole(role);
	}
	
	/**
	 * 跳转列表页面
	 * @return
	 */
	@RequestMapping(value="/listPage",method=RequestMethod.GET)
	public String listRolePage(){
		return "/permission/role/list";
	}
	
	/**
	 * 跳转新增页面
	 * @return
	 */
	@RequestMapping(value="/addPage",method=RequestMethod.GET)
	public String addRolePage(){
		return "/permission/role/add";
	}
	
	/**
	 * 跳转修改页面
	 * @return
	 */
	@RequestMapping(value="/updatePage",method=RequestMethod.GET)
	public ModelAndView updateRolePage(Integer id){
		ModelAndView mv=new ModelAndView("/permission/role/update");
		Result result=roleService.getRoleById(id);
		mv.addObject("role", result.getData());
		return mv;
	}
	
	/**
	 * 跳转权限分配页面
	 * @param roleId
	 * @return
	 */
	@RequestMapping(value="/permissionPage",method=RequestMethod.GET)
	public String permissionPage(Long roleId,Model model){
		model.addAttribute("roleId", roleId);
		return "/permission/role/rolemenu";
	}
	
	
	/**
	 * 根据角色id查询菜单
	 * @param roleId
	 * @return
	 */
	@RequestMapping(value="/getrolemenu",method=RequestMethod.POST)
	@ResponseBody
	public Object getRoleMenu(Long roleId){
		return roleService.getRoleMenuByRoleID(roleId);
	}
	
	
	/**
	 * 根据角色id查询菜单
	 * @param roleId
	 * @return
	 */
	@RequestMapping(value="/updateRoleMenu",method=RequestMethod.POST)
	@ResponseBody
	public Object updateRoleMenu(Long roleId,String menuIds){
		RoleMenuDto roleMenuDto = new RoleMenuDto();
		roleMenuDto.setRoleID(roleId);
		List<Long> list = new ArrayList<>();
		if (StringUtils.isNotBlank(menuIds)){
			String[] ids = menuIds.split(",");
			for (String id : ids) {
				list.add(Long.valueOf(id));
			}
			
		}
		roleMenuDto.setMenuID(list);
		return roleService.updateRoleMenu(roleMenuDto);
	}

	/**
	 * 启用角色
	 * @param role
	 * @return
	 */
	@RequestMapping(value="/enable",method=RequestMethod.POST)
	@ResponseBody
	public Object enable(@ModelAttribute Role role){
		try {
			role.setStatus(1);
			return roleService.enableOrDisableRole(role);
		} catch (Exception e) {
			logger.error("启用角色异常", e);
		}
		return getResult(ResultEnum.INTERNAL_SERVER_ERROR);
		
	}
	
	/**
	 * 禁用角色
	 * @param role
	 * @return
	 */
	@RequestMapping(value="/disable",method=RequestMethod.POST)
	@ResponseBody
	public Object disable(@ModelAttribute Role role){
		try {
			role.setStatus(2);
			return roleService.enableOrDisableRole(role);
		} catch (Exception e) {
			logger.error("禁用角色异常", e);
		}
		return getResult(ResultEnum.INTERNAL_SERVER_ERROR);
	}
	
}
